package com.youkeyi.ddy.cloud.basic.crm.source.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.crm.source.entity.SysCustomerSource;
import com.youkeyi.ddy.cloud.basic.crm.source.service.SysCustomerSourceService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.source.CrmSourceCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.source.dto.CrmSourceListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.source.dto.CrmSourceListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.source.param.CrmSourceCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.source.param.CrmSourceDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.source.param.CrmSourceQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.source.param.CrmSourceUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 客户来源表 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-04-07
 */
@Service
public class SysCustomerSourceController implements CrmSourceCloudService {
    
    @Autowired
    private SysCustomerSourceService sysCustomerSourceService;

    @Override
    public CrmSourceListCloudDto query(CrmSourceQueryCloudParam param) {
        CrmSourceListCloudDto dto = new CrmSourceListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getSourcePrimaryKeys())) {
            param.getSourcePrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCorpKeys())) {
            param.getCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCodes())) {
            param.getCodes().removeAll(Collections.singleton(null));
        }

        QueryWrapper<SysCustomerSource> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getSourcePrimaryKeys()), SysCustomerSource::getPrimaryKey, param.getSourcePrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getCorpKeys()), SysCustomerSource::getCorpKey, param.getCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getCodes()), SysCustomerSource::getCode, param.getCodes())
                .like(StringUtils.isNotBlank(param.getSourceFuzzy()), SysCustomerSource::getSource, param.getSourceFuzzy())
                .ne(SysCustomerSource::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;

        Page<SysCustomerSource> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<SysCustomerSource> sourceList = sysCustomerSourceService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(sourceList.getRecords())) {
            dto.setTotal(sourceList.getTotal());

            List<CrmSourceListItemCloudDto> items = new ArrayList<>();
            sourceList.getRecords().forEach(source -> {
                CrmSourceListItemCloudDto item = new CrmSourceListItemCloudDto();
                item.setSourcePrimaryKey(source.getPrimaryKey());
                item.setCorpKey(source.getCorpKey());
                item.setCode(source.getCode());
                item.setSource(source.getSource());
                item.setSourcePersonName(source.getSourcePersonName());
                item.setSourcePersonPhone(source.getSourcePersonPhone());
                item.setRemark(source.getRemark());
                item.setIsDeleted(source.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(source.getCreateTime() != null ? source.getCreateTime().getTime() : null);
                item.setCreateUserKey(source.getCreateUserKey());
                item.setCreateUserName(source.getCreateUserName());
                item.setUpdateTime(source.getUpdateTime() != null ? source.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(source.getUpdateUserKey());
                item.setUpdateUserName(source.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(CrmSourceCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param
                || StringUtils.isBlank(param.getCorpKey()) || StringUtils.isBlank(param.getSource())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        SysCustomerSource source = new SysCustomerSource();
        source.setPrimaryKey(SimpleUUidUtils.getUuid());
        source.setCorpKey(param.getCorpKey());
        source.setCode(param.getCode());
        source.setSource(param.getSource());
        source.setSourcePersonName(param.getSourcePersonName());
        source.setSourcePersonPhone(param.getSourcePersonPhone());
        source.setRemark(param.getRemark());
        source.setRemark(param.getRemark());
        source.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        source.setCreateUserKey(param.getCreateUserKey());
        source.setCreateUserName(param.getCreateUserName());
        source.setCreateTime(new Date());
        boolean save = sysCustomerSourceService.save(source);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(source.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(CrmSourceUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getSourcePrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        UpdateWrapper<SysCustomerSource> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(SysCustomerSource::getPrimaryKey, param.getSourcePrimaryKey());
        SysCustomerSource source = new SysCustomerSource();
        source.setPrimaryKey(param.getSourcePrimaryKey());
        source.setCode(param.getCode());
        source.setSource(param.getSource());
        source.setSourcePersonName(param.getSourcePersonName());
        source.setSourcePersonPhone(param.getSourcePersonPhone());
        source.setRemark(param.getRemark());
        source.setUpdateUserKey(param.getUpdateUserKey());
        source.setUpdateUserName(param.getUpdateUserName());
        source.setUpdateTime(new Date());
        boolean save = sysCustomerSourceService.update(source, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        return rc;
    }

    @Override
    public ResponseContent delete(CrmSourceDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || CollectionUtils.isEmpty(param.getSourcePrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        UpdateWrapper<SysCustomerSource> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .in(SysCustomerSource::getPrimaryKey, param.getSourcePrimaryKeys());

        SysCustomerSource source = new SysCustomerSource();
        source.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
        source.setUpdateUserKey(param.getUpdateUserKey());
        source.setUpdateUserName(param.getUpdateUserName());
        source.setUpdateTime(new Date());
        boolean save = sysCustomerSourceService.update(source, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
        }
        return rc;
    }
}
